<?php
/**
 *
 *
 *
 */
namespace Yz\modules\Uploads;

class Model_UploadImage
    extends Model_Upload
{

    /**
     *  @var image-resource
     */
    protected $image;

    /** Model's fields definition
     */
    protected function init()
    {
        debug();
        parent::init();
        $d =& $this->__dict__;
        $d['width']     = new \Yz_Property_Number();
        $d['height']    = new \Yz_Property_Number();
        return $this;
    }


    /**************************
     *      Public methods    *
     **************************/

    /**
     *
     *  @return Item
     *  @throw
     */
    public function processUpload($uploadName)
    {
        // TODO assign image parameters to action
        $action = $this
            ->selectAction(static::UPLOAD_ACTION)
            ->setUploadName($uploadName)
            ->setUploadVerifyCallback(array($this, 'verifyUpload'))
        ;
        $item = $this->_process($action);
        return $item->assignDimensionsFromImage($this->image);
    }


    /** the callback for upload verification in Action_Upload
     *  @return bool
     */
    public function verifyUpload($tempFile, $realFileName)
    {
        debug();
        $file = new \Yz_File($tempFile);
        $this->image = \Yz_Image_Default::fromFile($file);
        return true;
    }

    /*****************************
     *      Protected methods    *
     *****************************/

}
